import os
import re
from tqdm import tqdm
from collections import defaultdict

def process_log_file(input_path, output_path):
    with open(input_path, 'r', encoding='utf-8') as f:
        lines = f.readlines()
    
    if len(lines) < 2:
        with open(output_path, 'w', encoding='utf-8') as f_out:
            f_out.writelines(lines)
        return
    
    header = lines[:2]
    
    records = []
    for line in lines[2:]:
        line = line.strip()
        if not line:
            continue
        
        t_match = re.search(r'T<=>(\d+)', line)
        if t_match:
            t_value = int(t_match.group(1))
            records.append((t_value, line))
    
    records.sort(key=lambda x: x[0])
    
    with open(output_path, 'w', encoding='utf-8') as f_out:
        f_out.writelines(header)
        for t, line in records:
            f_out.write(line + '\n')

def process_all_logs(input_root, output_root):
    for date_dir in tqdm(os.listdir(input_root)):
        date_path = os.path.join(input_root, date_dir)
        if not os.path.isdir(date_path):
            continue
        
        output_date_path = os.path.join(output_root, date_dir)
        os.makedirs(output_date_path, exist_ok=True)
        
        for log_file in os.listdir(date_path):
            if not log_file.endswith('.txt'):
                continue
                
            input_file = os.path.join(date_path, log_file)
            output_file = os.path.join(output_date_path, log_file)
            
            try:
                process_log_file(input_file, output_file)
            except Exception as e:
                print(f"Error processing {input_file}: {str(e)}")

def data_processing():
    input_root = "data/behavior"
    output_root = "data/processed"
    os.makedirs(output_root, exist_ok=True)
    process_all_logs(input_root, output_root)

if __name__ == "__main__":
    data_processing()